package dsf.eshop.user.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

//@Component
//@Aspect
public class Logger {

    @Pointcut("execution(* dsf.eshop.user..*.*(..)) && !execution(* dsf.eshop.user.shiro.*.*(..))")
    public void all() {}

    @Around(value = "all()")
    public Object logger(ProceedingJoinPoint joinPoint) throws Throwable {

        Class<?> aClass = joinPoint.getTarget().getClass();
        org.slf4j.Logger logger = LoggerFactory.getLogger(aClass);
        String name = joinPoint.getSignature().getName();
        Method method = aClass.getMethod(name);
        logger.info("enter >>>>>>>>>>>> {}", method);
        Object result = null;
        try {
            result = joinPoint.proceed();
        } catch (Throwable throwable) {
            logger.error("{} {}", method, throwable.getMessage());
//            throw throwable;
        }
        logger.info("out <<<<<<<<<<< {}", method);

        return result;
    }

}
